package com.monitor.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.monitor.dao.ErpCustomerMapper;
import com.monitor.entity.ErpCustomer;
import com.monitor.service.ErpCustomerService;
import com.monitor.util.BlankUtil;
import com.monitor.util.Result;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 客户表 服务实现类
 * </p>
 *
 * @author LX
 * @since 2018-09-05
 */
@Service
@Transactional
public class ErpCustomerServiceImpl extends ServiceImpl<ErpCustomerMapper, ErpCustomer> implements ErpCustomerService {

    @Resource
    private ErpCustomerMapper customerMapper;


    @Override
    public Integer selectVehicletotalCount(Integer cusCode) {
        return customerMapper.selectVehicletotalCount(cusCode);
    }

    @Override
    public Map<String, BigDecimal> getCustomerCarNum(Integer cusCode) {
        return customerMapper.getCustomerCarNum(cusCode);
    }

    @Override
    public Map<String, Object> getCustomerCarNumByType(Integer cusCode, String type) {
        return customerMapper.getCustomerCarNumByType(cusCode,type);
    }

    @Override
    public List<ErpCustomer> selectCoutomerByType(Integer cusCode, String type) {
        return customerMapper.selectCoutomerByType(cusCode,type);
    }

    @Override
    public String selectCustType(ErpCustomer erpCustomer) {
        int catNum=0;
        if (null!=erpCustomer.getCusVehicletotal()&&null!=erpCustomer.getCusXCar())
        catNum=erpCustomer.getCusVehicletotal()-erpCustomer.getCusXCar();
        if(10>catNum)
            return "金牌客户";
        else
            return "VIP客户";
    }

    @Override
    public String getcustOfMe(Integer custId) {
        ErpCustomer customer=customerMapper.selectById(custId);
        List<ErpCustomer> customers=customerMapper.selectList(new EntityWrapper<ErpCustomer>().eq("cus_sup_company",customer.getCusCode()).or().eq("cus_service_com",customer.getCusCode()));
        customers.add(customer);
        List<Integer> custIds=customers.stream().map(ErpCustomer::getId).collect(Collectors.toList());
        String custIdsStr= BlankUtil.listToString(custIds);
        return custIdsStr;
    }

    @Override
    public Integer updateLogo(String logo, Integer id) {
        return customerMapper.updateLogo(logo,id);
    }

	@Override
	public Result<List<ErpCustomer>> selectByCusNameLike(String word) {
		// TODO Auto-generated method stub
		word="%"+word+"%";
		List<ErpCustomer> list= customerMapper.selectByCusNameLike(word);
		return new Result<>(list, list.size());
	}
}
